package com.mq.rabbitmq.point;

import com.mq.rabbitmq.point.consumewait.MultiData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 消息生产者
 *
 * @author liaokailin
 * @version $Id: Send.java, v 0.1 2015年11月01日 下午4:22:25 liaokailin Exp $
 */
@Component
public class Send implements RabbitTemplate.ConfirmCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;


//    @Bean
//    public Queue queue2() {
//        return new Queue("spring-boot-queue", true); //队列持久
//    }

    /**
     * 构造方法注入
     */
    @Autowired
    public Send() {
        //this.rabbitTemplate = rabbitTemplate;
        //rabbitTemplate.setConfirmCallback(this); //rabbitTemplate如果为单例的话，那回调就是最后设置的内容
    }

    /**
     * 不存在exchange会报错吗？
     * @param content
     */
    public void sendMsg(String content) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        //记住一定要有routingkey,不然找到不绑定对列的名称
        //rabbitTemplate.setMandatory(true);
        rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE ,AmqpConfig.ROUTINGKEY, content, correlationId);
    }

    public void seneMessage(String content) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        //记住一定要有routingkey,不然找到不绑定对列的名称
        //rabbitTemplate.setMandatory(true);

        rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE ,AmqpConfig.ROUTINGKEY, content, correlationId);
    }

    /**
     * 不存在exchange会报错吗？
     */
    public void sendSerialObject(Object object) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        //记住一定要有routingkey,不然找到不绑定对列的名称
        //rabbitTemplate.setMandatory(true);
        rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE ,AmqpConfig.ROUTINGKEY, object, correlationId);
    }

    public void sendToDefaultExchangeMsg(String content) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        //记住一定要有routingkey,不然找到不绑定对列的名称
        rabbitTemplate.convertAndSend(AmqpConfig.NO_BIND_QUEQU,"test");
//        rabbitTemplate.convertAndSend((Object) content, new MessagePostProcessor() {
//            @Override
//            public Message postProcessMessage(Message message) throws AmqpException {
//                return message;
//            }
//        });
    }

    /**
     * 回调
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println(" 回调idxx:" + correlationData);
        if (ack) {
            System.out.println("success");
        } else {
            System.out.println("error:" + cause);
        }
    }


    /**
     * 不存在exchange会报错吗？
     * @param content
     */
    public void sendToQueueContainsDead(String content) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        //记住一定要有routingkey,不然找到不绑定对列的名称
        //rabbitTemplate.setMandatory(true);
        rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE ,AmqpConfig.ROUTINGKEY, content, correlationId);
    }


    public void sendMsgForTestOneConsumeExceptionOfMultiConsume(MultiData data) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        //记住一定要有routingkey,不然找到不绑定对列的名称
        rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE ,AmqpConfig.ROUTINGKEY, data, correlationId);
    }
}